package cn.gxa.j229.bug.finance.controller.borrowing;

import cn.gxa.j229.bug.finance.BusinessException;
import cn.gxa.j229.bug.finance.pojo.Loan;
import cn.gxa.j229.bug.finance.service.borrowing.LoanService;
import cn.gxa.j229.bug.finance.utils.JsonResult;
import cn.gxa.j229.bug.finance.utils.PageUtils;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/loan")
public class LoanController {
    @Resource
    LoanService loanService = null;
    JsonResult jsonResult = new JsonResult();

    @Transactional(propagation = Propagation.REQUIRES_NEW,isolation = Isolation.DEFAULT,rollbackFor = {Throwable.class})
    @PostMapping("/insert")
    @ResponseBody
    public Map insertLoan(@RequestBody Loan loan){
        int insertSelective = loanService.insertSelective(loan);
        jsonResult.setCode("200");
        jsonResult.setMsg("新增借款成功");
        return jsonResult.getValues();
    }

    @Transactional(propagation = Propagation.SUPPORTS,isolation = Isolation.DEFAULT,rollbackFor = {Throwable.class})
    @PostMapping("/find")
    @ResponseBody
    public Map findLoan(@RequestBody Loan loan){
        List<Map<String, Object>> loanServiceById = loanService.findById(loan.getId());
        jsonResult.setCode("200");
        jsonResult.setData(loanServiceById);
        jsonResult.setMsg("成功");
        return jsonResult.getValues();
    }

    @Transactional(propagation = Propagation.SUPPORTS,isolation = Isolation.DEFAULT,rollbackFor = {Throwable.class})
    @PostMapping("/update")
    @ResponseBody
    public Map updateLoan(@RequestBody Loan loan){
        int update = loanService.update(loan);
        jsonResult.setCode("200");
        jsonResult.setMsg("提交成功");
        jsonResult.setData(loan);
        return jsonResult.getValues();
    }

    @Transactional(propagation = Propagation.SUPPORTS,isolation = Isolation.DEFAULT,rollbackFor = {Throwable.class})
    @PostMapping("/updateByNum")
    @ResponseBody
    public Map updateLoanByNum(@RequestBody Loan loan){
        int i = loanService.updateByNumSelective(loan);
        jsonResult.setCode("200");
        jsonResult.setMsg("提交成功");
        jsonResult.setData(loan);
        return jsonResult.getValues();
    }

    @Transactional(propagation = Propagation.SUPPORTS,isolation = Isolation.DEFAULT,rollbackFor = {Throwable.class})
    @PostMapping("/updating")
    @ResponseBody
    public Map updating(@RequestBody Loan loan) {
        int loanServiceByPage = loanService.updating(loan);
        jsonResult.setCode("200");
        jsonResult.setMsg("成功");
        jsonResult.setData(loanServiceByPage);
        return jsonResult.getValues();
    }

    @Transactional(propagation = Propagation.SUPPORTS,isolation = Isolation.DEFAULT,rollbackFor = {Throwable.class})
    @PostMapping("/fullupdate")
    @ResponseBody
    public Map fullUpdate(@RequestBody Loan loan) throws BusinessException {
        if (loan.getPasss() == null){
            throw new BusinessException("50005","请确认是否通过。passs不能为空！！！");
        }
        if (loan.getPasss().equals("是")){
            loan.setPass(1);
        }else {
            loan.setPass(0);
        }
        int fullupdate = loanService.fullupdate(loan);
        jsonResult.setCode("200");
        jsonResult.setMsg("成功");
        jsonResult.setData(loan);
        return jsonResult.getValues();
    }

    @Transactional(propagation = Propagation.SUPPORTS,isolation = Isolation.DEFAULT,rollbackFor = {Throwable.class})
    @PostMapping("/delete")
    @ResponseBody
    public Map deleteLoan(@RequestBody Loan loan){
        int deleteByNum = loanService.deleteByNum(loan.getNum());
        jsonResult.setCode("200");
        jsonResult.setMsg("已作废");
        jsonResult.setData("作废了：" + deleteByNum+ "条数据");
        return jsonResult.getValues();
    }

    @Transactional(propagation = Propagation.SUPPORTS,isolation = Isolation.DEFAULT,rollbackFor = {Throwable.class})
    @PostMapping("/findByPage")
    @ResponseBody
    public Map findByPageLoan(@RequestBody PageUtils pageUtils){
        Integer limit = pageUtils.getLimit();
        Integer page = pageUtils.getPage();
//        loanService.findByPage(limit, page)
        List<Map<String, Object>> loanServiceByPage = loanService.findByPage(limit, page);
//        Loan loan = JSON.parseObject(JSON.toJSONString(loanServiceByPage), Loan.class);
//        Double speed = loan.getSpeed();
//        if (speed == 1){
//            loan.setState(2);
//        }
//        List<Map<String, Object>> loanServiceByPage2 = loanService.findByPage(limit, page);
        Integer counts = loanService.findByPageCount();
        jsonResult.setData(loanServiceByPage);
        jsonResult.put("count",counts);
        jsonResult.setCode("200");
        jsonResult.setMsg("成功");
        return jsonResult.getValues();
    }

    @Transactional(propagation = Propagation.SUPPORTS,isolation = Isolation.DEFAULT,rollbackFor = {Throwable.class})
    @PostMapping("/findByPhone")
    @ResponseBody
    public Map findByPhone(@RequestBody Loan loan) {
        List<Map<String, Object>> loanServiceByPage = loanService.findByPhone(loan.getPhone());
        jsonResult.setCode("200");
        jsonResult.setMsg("成功");
        jsonResult.setData(loanServiceByPage);
        return jsonResult.getValues();
    }

    @Transactional(propagation = Propagation.SUPPORTS,isolation = Isolation.DEFAULT,rollbackFor = {Throwable.class})
    @PostMapping("/findByBorrower")
    @ResponseBody
    public Map findByBorrower(@RequestBody Loan loan) {
        List<Map<String, Object>> loanServiceByBorrower = loanService.findByBorrower(loan.getBorrower());
        jsonResult.setCode("200");
        jsonResult.setMsg("成功");
        jsonResult.setData(loanServiceByBorrower);
        return jsonResult.getValues();
    }

    @Transactional(propagation = Propagation.SUPPORTS,isolation = Isolation.DEFAULT,rollbackFor = {Throwable.class})
    @PostMapping("/findingByPage")
    @ResponseBody
    public Map findingByPageLoan(@RequestBody Loan loan){
//        Integer limit = pageUtils.getLimit();
//        Integer page = pageUtils.getPage();
//        loanService.findByPage(limit, page)
        List<Map<String, Object>> loanServiceByPage = loanService.findingByPage(loan);
        Integer counts = loanService.findingByPageCount(loan);
        jsonResult.setData(loanServiceByPage);
        jsonResult.put("count",counts);
        jsonResult.setCode("200");
        jsonResult.setMsg("成功");
        return jsonResult.getValues();
    }

    @Transactional(propagation = Propagation.SUPPORTS,isolation = Isolation.DEFAULT,rollbackFor = {Throwable.class})
    @PostMapping("/findFinishByPage")
    @ResponseBody
    public Map findFinishByPageLoan(@RequestBody Loan loan){
//        Integer limit = pageUtils.getLimit();
//        Integer page = pageUtils.getPage();
//        loanService.findByPage(limit, page)
        List<Map<String, Object>> loanServiceByPage = loanService.findFinishByPage(loan);
        Integer counts = loanService.findFinishByPageCount(loan);
        jsonResult.setData(loanServiceByPage);
        jsonResult.put("count",counts);
        jsonResult.setCode("200");
        jsonResult.setMsg("成功");
        return jsonResult.getValues();
    }

    @Transactional(propagation = Propagation.SUPPORTS,isolation = Isolation.DEFAULT,rollbackFor = {Throwable.class})
    @PostMapping("/findAll")
    @ResponseBody
    public Map findAllLoan(){
        List<Map<String, Object>> loanServiceAll = loanService.findAll();
        jsonResult.setData(loanServiceAll);
        jsonResult.setCode("200");
        jsonResult.setMsg("成功");
        return jsonResult.getValues();
    }

    @Transactional(propagation = Propagation.SUPPORTS,isolation = Isolation.DEFAULT,rollbackFor = {Throwable.class})
    @PostMapping("/findLikeLoanDefend")
    @ResponseBody
    public Map findLikeLoanDefend(@RequestBody Loan loan){
        List<Map<String, Object>> likeLoanDefend = loanService.findLikeLoanDefend(loan);
        Integer counts = loanService.findLikeLoanDefendCount(loan);
        jsonResult.put("count",counts);
        jsonResult.setCode("200");
        jsonResult.setData(likeLoanDefend);
        jsonResult.setMsg("成功");
        return jsonResult.getValues();
    }

    @Transactional(propagation = Propagation.SUPPORTS,isolation = Isolation.DEFAULT,rollbackFor = {Throwable.class})
    @PostMapping("/findLikeLoanCheck")
    @ResponseBody
    public Map findLikeLoanCheck(@RequestBody Loan loan){
        List<Map<String, Object>> likeLoanCheck = loanService.findLikeLoanCheck(loan);
        Integer counts = loanService.findLikeLoanCheckCount(loan);
        jsonResult.put("count",counts);
        jsonResult.setCode("200");
        jsonResult.setData(likeLoanCheck);
        jsonResult.setMsg("成功");
        return jsonResult.getValues();
    }

}
